package com.lancoo.znbkxx.adapter;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.lancoo.znbkxx.R;
import com.lancoo.znbkxx.net.MyCollectItemBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by TangJun on 2019/2/20
 */
public class MyCollectAdapter extends RecyclerView.Adapter<MyCollectAdapter.ViewHolder>{

    /**
     * 展示数据
     */
    private List<MyCollectItemBean> mData;
    private StringBuilder keyString;
    /**
     * 事件回调监听
     */
    private MyCollectAdapter.OnItemClickListener onItemClickListener;
    private MyCollectAdapter.OnDeleteClickLister mDeleteClickListener;

    public MyCollectAdapter(List<MyCollectItemBean> data) {
        this.mData = data;
        keyString = new StringBuilder();
    }

    public void updateData(List<MyCollectItemBean> data) {
        this.mData = data;
        notifyDataSetChanged();
    }
    public void setKeyString(String key) {
        if (TextUtils.isEmpty(keyString.toString())) {
            keyString.append(key);
        } else {
            keyString.setLength(0);
            keyString.append(key);
        }
    }
    /**
     * 添加新的Item
     */
    public void addNewItem() {
        if(mData == null) {
            mData = new ArrayList<>();
        }
        //mData.add(0, "new Item");
        notifyItemInserted(0);
    }

    /**
     * 删除Item
     */
    public void deleteItem(int position) {
        if(mData == null || mData.isEmpty()) {
            return;
        }
        mData.remove(position);
        notifyItemRemoved(position);
    }

    /**
     * 设置回调监听
     *
     * @param listener
     */
    public void setOnItemClickListener(MyCollectAdapter.OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    @Override
    public MyCollectAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_collect, parent, false);
        // 实例化viewholder
        MyCollectAdapter.ViewHolder viewHolder = new MyCollectAdapter.ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.tvName.setText(mData.get(position).getGenreName());
        holder.tvContent.setText(mData.get(position).getQuesBrief());
        holder.tvTime.setText(mData.get(position).getCreateTime());
       /* if (!holder.tvDelete.hasOnClickListeners()) {
            holder.tvDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mDeleteClickListener != null) {
                        mDeleteClickListener.onDeleteClick(v, position);
                    }
                }
            });
        }*/
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                if(onItemClickListener != null) {
                    int pos = holder.getLayoutPosition();
                    onItemClickListener.onItemClick(holder.itemView, pos);
                }
            }
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if(onItemClickListener != null) {
                    int pos = holder.getLayoutPosition();
                    onItemClickListener.onItemLongClick(holder.itemView, pos);
                }
                //表示此事件已经消费，不会触发单击事件
                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvContent;
        TextView tvName;
        TextView tvTime;

        public ViewHolder(View itemView) {
            super(itemView);
            tvContent = itemView.findViewById(R.id.tv_content);
            tvName = itemView.findViewById(R.id.tv_name);
            tvTime = itemView.findViewById(R.id.tv_time);

        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
        void onItemLongClick(View view, int position);
    }
    // protected abstract void fillItemData(WrongItemAdapter.ViewHolder viewHolder, int position);

    public void setOnDeleteClickListener(OnDeleteClickLister listener) {
        this.mDeleteClickListener = listener;
    }

    public interface OnDeleteClickLister {
        void onDeleteClick(View view, int position);
    }
}
